package com.chushouya.manager.dao.repository;

import com.general.framework.core.enums.DelFlagEnum;
import com.chushouya.manager.dao.entity.AdminUserEntity;
import com.chushouya.manager.dao.mapper.AdminUserEntityMapper;
import com.chushouya.manager.dto.admin.user.AdminUserQuery;
import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class AdminUserRepository extends BaseRepository<AdminUserEntityMapper, AdminUserEntity, Long> {

    public PageResult<AdminUserEntity> selectUserPageResult(AdminUserQuery query) {
        return this.selectPage(query.getPageRequest(), this.buildExample(query));
    }
    public List<AdminUserEntity> selectUserList(AdminUserQuery query) {
        return this.selectByExample(this.buildExample(query));
    }

    private Example buildExample(AdminUserQuery query) {
        Example example = this.buildExample();
        if (Strings.isNotEmpty(query.getUserName())) {
            example.and().andEqualTo("userName", query.getUserName());
        }
        if (Strings.isNotEmpty(query.getKeyword())) {
            example.and().orLike("userName", Strings.format("%{}%", query.getKeyword()))
                    .orLike("nickName", Strings.format("%{}%", query.getKeyword()))
                    .orLike("phoneNumber", Strings.format("%{}%", query.getKeyword()));
        }
        if (Strings.isNotEmpty(query.getUserNameLike())) {
            example.and().andLike("userName", Strings.format("%{}%", query.getUserNameLike()));
        }
        if (Strings.isNotEmpty(query.getNickName())) {
            example.and().andLike("nickName", Strings.format("%{}%", query.getNickName()));
        }
        if (Strings.isNotEmpty(query.getPhoneNumber())) {
            example.and().andLike("phoneNumber", Strings.format("%{}%", query.getPhoneNumber()));
        }
        if (Objects.nonNull(query.getStatus())) {
            example.and().andEqualTo("status", query.getStatus());
        }
        if (Lists.isNotEmpty(query.getUserIdIdList())) {
            example.and().andIn("adminUserId", query.getUserIdIdList());
        }
        if (Lists.isNotEmpty(query.getNotInUserIdIdList())) {
            example.and().andNotIn("adminUserId", query.getNotInUserIdIdList());
        }
        if (Objects.nonNull(query.getIdNotEqual())) {
            example.and().andNotEqualTo("adminUserId", query.getIdNotEqual());
        }
        if (Lists.isNotEmpty(query.getDeptIdList())) {
            example.and().andIn("deptId", query.getDeptIdList());
        }
        if (Objects.nonNull(query.getUserType())) {
            example.and().andEqualTo("userType", query.getUserType());
        }
        example.and().andEqualTo("delFlag", DelFlagEnum.normal.getValue());
        example.orderBy("adminUserId").asc();
        return example;
    }

    public AdminUserEntity selectOne(AdminUserQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    public Integer getAdminUserCount(AdminUserQuery query) {
        return this.selectCountByExample(this.buildExample(query));
    }

    public List<AdminUserEntity> selectAdminUserList(AdminUserQuery query) {
        return this.selectPageList(query.getPageRequest(), this.buildExample(query));
    }

    public Integer selectDeptUserCount(Long deptId) {
        Example example = buildExample();
        example.and().andEqualTo("deptId", deptId);
        return this.selectCountByExample(example);
    }
}
